<template>
	<view class="container">

		<scroll-view scroll-y="true" class="scroll-view">
			<uni-swipe-action>
				<uni-swipe-action-item v-for="(activity, index) in activities" :key="activity.activityId" class="activity-item"
					:right-options="options" @click="onClick($event,activity)">
					<view class="" @click="toDetail(activity.activityId)">
						<view class="activity-title">
							<text>活动{{index+1}}:{{ activity.activityTitle }}</text>
						</view>
						<view class="activity-image-container">
							<image class="activity-image" :src="activity.activityImage" mode="aspectFill"></image>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</scroll-view>


		<!-- 添加按钮 -->
		<view class="add-btn" @click="toAdd">
			发布
		</view>
		<!-- 自定义导航栏 -->
		<u-tabbar :value="value" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true">
			<u-tabbar-item text="首页" icon="home" @click="toIndex"></u-tabbar-item>
			<u-tabbar-item text="活动" icon="more-circle" @click="toActivity"></u-tabbar-item>
			<u-tabbar-item text="用户" icon="account" @click="toUser"></u-tabbar-item>
			<u-tabbar-item text="商家" icon="bag" @click="toGoods"></u-tabbar-item>
		</u-tabbar>
	</view>
</template>

<script>
	import store from '@/store/index.js'
	import {
		mapState,
		mapMutations,
		mapActions,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				value: 1,
				options: [
					// 	{
					// 	text: '编辑',
					// 	style: {
					// 		backgroundColor: '#007aff'
					// 	}
					// },
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				],
			};
		},
		methods: {
			...mapMutations('admin', ['getActivities']),

			toAdd() {
				// console.log(e);
				uni.navigateTo({
					url: '/pages/admin/activity/activity-add/activity-add',
				});
			},
			// 侧边
			onClick(e, item) {
				console.log(e, item)
				// if (e.index == 0) {
				// 	console.log(0)
				// }
				if (e.index == 0) {
					uni.request({
						url: 'http://localhost:3000/course/delete',
						method: 'post',
						data: [item, "activity"],
						success: (res) => {
							console.log("删除数据：", res);
							this.getActivities()
						},
						fail: (res) => {
							console.log(res)
						}
					})
				}
			},
			// 参与活动的方法，这里需要传入活动的id（或其他唯一标识）
			joinActivity(activityId) {
				uni.showToast({
					title: `你已成功参与活动ID为${activityId}的活动！`,
					icon: "success",
				});
				// 跳转到其他页面或执行其他逻辑，可以根据activityId来处理
			},
			toDetail(id) {
				console.log("id:" + id);
				uni.navigateTo({
					url: `/pages/activity/activity-detail/activity-detail?id=${id}`,
				});
			},
			//导航栏跳转
			toIndex() {
				uni.reLaunch({
					url: '/pages/admin/admin'
				})
			},
			toActivity() {
				uni.reLaunch({
					url: '/pages/admin/activity/activity'
				})
			},
			toUser() {
				uni.reLaunch({
					url: '/pages/admin/user/user'
				})
			},
			toGoods() {
				uni.reLaunch({
					url: '/pages/admin/merchant/merchant'
				})
			}
		},
		computed: {
			...mapState('admin', ['activities']),

		},
		beforeMount() {
			if (!this.activities.length) {
				console.log(Boolean(this.activities.length), "数据为空获取数据")
				this.getActivities()
			}
		}
	};
</script>

<style scoped>
	.container {
		padding: 20px;
	}

	.scroll-view {
		height: 100%;
		/* 或者设置为具体的高度值 */
	}

	.activity-item {
		margin-bottom: 20rpx;
		border-bottom: 1px solid #ddd;
		padding-bottom: 30rpx;
	}

	.activity-title {
		font-size: 52rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.activity-image-container {
		width: 100%;
		height: 400rpx;
		/* 根据需要调整图片高度 */
		margin-bottom: 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.activity-image {
		width: 100%;
		height: 100%;
		object-fit: cover;
		/* 使图片保持比例填充容器 */
	}

	.activity-description {
		font-size: 36rpx;
		color: #333;
		line-height: 1.5;
		text-align: justify;
	}

	.add-btn {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 100rpx;
		z-index: 2;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		margin: 30rpx 20rpx;
		color: #fff;
		border-radius: 80rpx;
		font-size: 30rpx;
		background-color: #27ba9b;
	}
</style>